from django.utils.decorators import method_decorator
from user.models import Role
from django.views import View
from django.http.response import JsonResponse
from django_system.role_recognize import check_permission


class RoleViewSet(View):
    """获取角色权限"""

    @method_decorator(check_permission(['管理员']))
    def get(self, request, id):
        role = Role.objects.get(id=id)
        info = {'name': role.name, 'id': role.id}
        p_list = [i['id'] for i in list(role.permissions.filter(is_delete=False).all().values())]
        return JsonResponse({'code': 200, 'message': 'OK!~~~~~', 'data': info, 'permissions': p_list})
